package com.wang.controller;


import com.wang.base.http.RestResponse;
import com.wang.domain.LargeCategory;
import com.wang.service.LargeCategoryService;
import com.wang.service.MedicineService;
import com.wang.service.SmallCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WangZai
 * @since 2023-05-06
 */
@CrossOrigin
@RestController
@RequestMapping("/td/api/largeCategory")
@Api(tags = "大类模块接口说明")
public class LargeCategoryController {
    private final ModelMapper modelMapper;
    private final MedicineService medicineService;
    private final LargeCategoryService largeCategoryService;
    private final SmallCategoryService smallCategoryService;

    @Autowired
    public LargeCategoryController(ModelMapper modelMapper, MedicineService medicineService, LargeCategoryService largeCategoryService, SmallCategoryService smallCategoryService) {
        this.modelMapper = modelMapper;
        this.medicineService = medicineService;
        this.largeCategoryService = largeCategoryService;
        this.smallCategoryService = smallCategoryService;
    }

    @Autowired


    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/getAllData")
    @ApiOperation("管理员获取所有大类数据接口说明")
    public RestResponse<Object> getAllData() {
        List<LargeCategory> largeCategoryList = largeCategoryService.getLargeCategories();
        if (largeCategoryList.isEmpty()) {
            return RestResponse.failure("无大类标签");
        } else {
            return RestResponse.ok(largeCategoryList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/addData")
    @ApiOperation("管理员添加大类数据接口说明")
    public RestResponse<Object> addData(@RequestParam("largeCategoryName") String largeCategoryName) {
        LargeCategory largeCategory = new LargeCategory();
        largeCategory.setLargeCategoryName(largeCategoryName);
        if (largeCategoryService.getLargeCategoryByName(largeCategoryName) == null) {
            largeCategoryService.addLargeCategory(largeCategory);
            return RestResponse.ok("添加成功！");
        } else {
            return RestResponse.failure("名称重复，添加失败！！！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/deleteData/{largeCategoryName}")
    @ApiOperation("管理员删除大类及下属数据接口说明")
    public RestResponse<Object> deleteData(@PathVariable String largeCategoryName) {
        LargeCategory largeCategory = largeCategoryService.getLargeCategoryByName(largeCategoryName);
        if (largeCategoryService.deleteLargeCategory(largeCategory.getLargeCategoryId()) && smallCategoryService.deleteSmallCategoryByLargeCategoryId(largeCategory.getLargeCategoryId()) && medicineService.deleteMedicineByLargeCategoryId(largeCategory.getLargeCategoryId())) {
            return RestResponse.ok("删除成功！");
        } else {
            return RestResponse.failure("删除失败！！！");
        }
    }
}

